package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.CustomerMapper;
import com.hqyj.dao.OrderMapper;
import com.hqyj.pojo.Customer;
import com.hqyj.pojo.DataMessage;
import com.hqyj.pojo.Order;
import com.hqyj.pojo.User;
import com.hqyj.service.TransactionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TransactionServiceImpl implements TransactionService {

    @Resource
    OrderMapper orderMapper;

    @Resource
    CustomerMapper customerMapper;


    @Override
    public DataMessage list(Order o) {
        DataMessage data =new DataMessage();
        //1.定义分页参数对象：startPage(页码，每页显示条数)
        PageHelper.startPage(o.getPage(),o.getRow());
        List<Order> list =orderMapper.queryByOrder(o);
        //创建分页结果集对象
        PageInfo<Order> pageResult =new PageInfo<Order>(list);
        //判断分页结果集是否有对象
        if(pageResult!=null){
            Map<String,Object> map =new HashMap<>();
            //当前页集合
            map.put("list",pageResult.getList());
            //总页数
            map.put("totalPage",pageResult.getPages());
            //总条数
            map.put("total",pageResult.getTotal());
            data.setSuccess(true);
            data.setData(map);
        }else {
            data.setSuccess(false);
            data.setInfo("查询失败");
        }
        return data;
    }

    @Override
    public DataMessage Add(Order o, Customer c) {

            DataMessage data =new DataMessage();
            int num =orderMapper.insert(o);


            if(num>0){
                data.setSuccess(true);
                data.setInfo("增加成功");


            }else {
                data.setSuccess(false);
                data.setInfo("增加失败");
            }
            return data;

    }

    @Override
    public DataMessage update(Order o) {
        DataMessage data =new DataMessage();
        int num =orderMapper.updateByPrimaryKey(o);
        if(num>0){
            data.setSuccess(true);
            data.setInfo("修改成功");
        }else {
            data.setSuccess(false);
            data.setInfo("修改失败");
        }
        return data;
    }


}
